﻿@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService

<CnGalWebSite.Components.Containers.MasaTitleContainer Title="可选标签" Icon="@IconType.Search.ToIconString()" Collapable Class="@Class">
    <MTextField Value="_search" ValueChanged="OnSearch" TValue="string" Class="ps-3 pe-3"
                Label="可以搜索标签哦~"
                Clearable
                ClearIcon="mdi-close-circle-outline"></MTextField>
    <MTreeview Items="Items"
               Search="@_search"
               TItem="TagTreeModel"
               TKey="string"
               ItemKey="r => r.Title"
               ItemText="r => r.Title"
               ItemChildren="r => r.Children"
               Activatable
               Selectable="Model!=null"
               ValueChanged="ValueChanged"
               Value="selectedItems">
        <PrependContent>
            @if (string.IsNullOrWhiteSpace(context.Item.Icon) == false)
            {
                <MIcon Color="primary">
                    @context.Item.Icon
                </MIcon>
            }
        </PrependContent>
    </MTreeview>
</CnGalWebSite.Components.Containers.MasaTitleContainer>




@code {
    [Parameter]
    public string Class { get; set; }

    [Parameter]
    public EventCallback<string> OnInput { get; set; }
    [Parameter]
    public List<RelevancesModel> Model { get; set; }

    private List<string> SearchResult = new List<string>();
    private List<TagTreeModel> Items { get; set; } = new List<TagTreeModel>();
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    private string _search;

    private List<string> lastTags = new List<string>();
    private List<string> selectedItems = new List<string>();

    protected override async Task OnInitializedAsync()
    {
        try
        {
            //获取所有标签
            Items = await _httpService.GetAsync<List<TagTreeModel>>("api/tags/GetTagsTreeView");

            //遍历寻找最后一级的标签
            foreach (var item in Items)
            {
                AddLastTags(item.Children);
            }


        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取树形标签数据失败");
        }
    }

    public void AddLastTags(List<TagTreeModel> model)
    {
        foreach (var item in model)
        {
            if (item.Children.Any())
            {
                AddLastTags(item.Children);
            }
            else
            {
                lastTags.Add(item.Title);
            }
        }
    }

    public void Refresh()
    {
        if (Model != null)
        {
            selectedItems = Model.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName).ToList();
        }
        else
        {
            selectedItems.Clear();
        }
        StateHasChanged();
        
    }

    public async Task ValueChanged(List<string> items)
    {
        items.RemoveAll(s => lastTags.Contains(s) == false);
        selectedItems = items;

        if (Model != null)
        {
            Model.RemoveAll(s => items.Contains(s.DisplayName) == false && lastTags.Contains(s.DisplayName));
            Model.AddRange(items.Where(s => Model.Select(s => s.DisplayName).Contains(s) == false).Select(s => new RelevancesModel { DisplayName = s }));
        }

        await OnInput.InvokeAsync();

    }

    private async Task OnSearch(string text)
    {
        _search = text;
    }
}
